/*
May 3, 2013
ImageBoard.java
Erich Hauck erik Kringlen
 */
/*
May 3, 2013
ImageBoard.java
Erich Hauck
 */
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class ImageBoard extends JPanel implements ActionListener, KeyListener, MouseListener
{
   /* ::::::::::PROBLEMS:::::::
   -When you are walking, the monster's sprite doesn't change.
   -There is something wrong with the collision, when the boxes collide hp doesnt always go down. 
   Has to do with timer of 100
   */
   
   
   
   
   //used for character movement
   private Timer animator;
   Rectangle player, monster, obstcle;
   private int /*milisecond delay*/ delay=10;
   //creates the images for classes
   Player p = new Player();
   Map map = new Map();
   
   Monster creepers;
   ArrayList<Monster> monsters = new ArrayList<Monster>();
   ArrayList<Obstacles> obstaclez = new ArrayList<Obstacles>();
   RangeWeapon rw0 = new RangeWeapon();
   RangeWeapon rw1 = new RangeWeapon();
   RangeWeapon rw2 = new RangeWeapon();
   Door door;

   Assets life = new Assets(); 
   Weapon w = new Weapon();
   Obstacles obstacle;
   //doesnt let you get hurt for a certain amount of time
   int harmCount=0;
   
   //sees if the monster and obstacle spawn
   boolean monsterSpawn=true, obstacleSpawnPit=true, obstacleSpawnLavaPit = true;
   //Counts your scores
   int levelDown =0, monstersKilled=0;
   //sees what level you are on
   int levelcount;
   //monster and pplayers x and y values
   public int playerX, playerY, weaponX, weaponY;
   public int startX, startY, startX1, startY1, startX2, startY2;
   //runs counters and frames
   int counter, monstcount, monstframe, counterMONST, weapcount=0, weaponframecount, bowCount, bowCount1, bowCount2, arrowCount, coolDown, killReach=0;
   
   //sees how many monsters you have
   int monsterAmount;
   //sees how many  obstacles there are
   int obstacleAmount;
   //determines how many will spawn
   int spawnAmount=5;
   //checks if you are still in the game
   boolean inGame = false;
   Random rand = new Random();
   
   public ImageBoard()
   {
      //sets random amount of monsters and obstacles
      monsterAmount=rand.nextInt(5)+spawnAmount;
      obstacleAmount = rand.nextInt(3)+spawnAmount*2;
      
      //creates x amount of monsters
      for(int i=0; i<monsterAmount; i++)
      {
         //monsters
         
         creepers = new Monster();
         int s12, s13;
         do
         	{
         	 s12 = rand.nextInt(600);
              s13 = rand.nextInt(600);
         	}
         while((s12>200&&s12<400)&&(s13>450));         
         creepers.setX(s12);
         creepers.setY(s13);
         monsters.add(creepers);
         
      }
      //creates a random amount of obstacles
      for(int i=0; i<obstacleAmount; i++)
      {
         obstacle = new Obstacles();
         //obstacle
         int z, q;
         do
         	{
         	 z = rand.nextInt(600);
             q = rand.nextInt(600);
         	}
         while((z>200&&z<400)&&(q>450));      
         obstacle.setX(z);
         obstacle.setY(q);
         
         obstaclez.add(obstacle);
      }
      //creates the door 
      door = new Door();
      door.setX(rand.nextInt(600));
      door.setY(rand.nextInt(600));
      //starting the timer
      animator = new Timer(delay, this);
      animator.start();
      counter =0;
      monstcount=0;
      monstframe=0;
      weapcount=0;
      bowCount=0;
      bowCount1=0;
      bowCount2=0;
      arrowCount=1;
      coolDown=100;
      
      //creates the monster and player boundries
      player = p.getBounds();
      //players x an y location
      playerX = p.getX();
      playerY= p.getY();
      
      
      
      //lets the listener work
      addMouseListener(this);
      addKeyListener(this);
      setFocusable(true);
      setFocusTraversalKeysEnabled(false);
   }
   
   

      
   public void paintComponent(Graphics g) 
   {
	   
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      if (inGame==false)
      {
    	  g2.setColor(Color.BLACK);
    	  g2.drawRect(0,0,600,600);
    	  g2.fillRect(0,0,600,600);
    	  g2.setColor(Color.RED);
    	  g2.fillRect(200, 100, 200, 100);
    	  g2.setColor(Color.YELLOW);
    	  Font font = new Font(Font.SANS_SERIF, Font.BOLD, 24);
          g2.setFont(font);
          if(levelDown>0||monstersKilled>0)
    	  g2.drawString("play Again?", 225, 150);
          else
        	  g2.drawString("Enter Game!", 225, 150);
      }
      
      if(inGame)
      {
      g2.drawImage(map.getImage(), 0, 0, this);

      if(obstacleSpawnLavaPit==true)
      {
         for(int i =0; i<obstacleAmount; i++)
         {
            g2.drawImage(obstaclez.get(i).getLavaPit(), obstaclez.get(i).getX(), obstaclez.get(i).getY(), this);
         }
      }
      g2.drawImage(door.getImage(), door.getX(), door.getY(), this);
      
      if(monsterSpawn==true)
         for (int i = 0; i < monsters.size(); i++) 
         {
            if(monsters.get(i).getLife()==true)
            {
            g2.drawImage(monsters.get(i).getSprite(), monsters.get(i).getX(), monsters.get(i).getY(), this );
            }
         }
      
      g2.drawImage(p.character(), playerX, playerY, this);

      
      g2.setColor(Color.YELLOW);
      g2.drawRect(120, 5, 100, 20);
      g2.fillRect(120, 5, 0+coolDown, 20);
      
      g2.setColor(Color.GREEN);
      int endX, endX1, endX2;
      double bCountY, bCountX, bCountY1, bCountX1, bCountY2, bCountX2;
      
      if(rw0.firing())
      {
          bowCount+=15;
          if (rw0.sideOfPlayer(p.getX()))
          {
              endX = startX +(int)rw0.fireX();
              bCountX=rw0.firingX(bowCount);
          } 
          else
          {
              endX = startX -(int)rw0.fireX();
              bCountX=-(rw0.firingX(bowCount));
          }
          
           bCountY=(rw0.firingY(bowCount));
          
          g2.drawLine(startX+(int)bCountX, startY+(int)bCountY, endX+(int)bCountX, startY+(int)rw0.fireY()+(int)bCountY);
          rw0.changeBounds(endX+(int)bCountX-1, startY+(int)rw0.fireY()+(int)bCountY-1);
          g2.draw(rw0.getBounds());
          
          if(endX+(int)bCountX>600||endX+(int)bCountX<0||startY+(int)rw0.fireY()+(int)bCountY>600||startY+(int)rw0.fireY()+(int)bCountY<0)
              {
              rw0.setFiring(false);
                rw0.changeBounds(-5, -5);
              }
      }
      
      if(rw1.firing())
      {
          bowCount1+=15;
          if (rw1.sideOfPlayer(p.getX()))
          {
              endX1 = startX1 +(int)rw1.fireX();
              bCountX1=rw1.firingX(bowCount1);
          } 
          else
          {
              endX1 = startX1 -(int)rw1.fireX();
              bCountX1=-(rw1.firingX(bowCount1));
          }
          
           bCountY1=(rw1.firingY(bowCount1));
          
          g2.drawLine(startX1+(int)bCountX1, startY1+(int)bCountY1, endX1+(int)bCountX1, startY1+(int)rw1.fireY()+(int)bCountY1);
          rw1.changeBounds(endX1+(int)bCountX1-1, startY1+(int)rw1.fireY()+(int)bCountY1-1);
          g2.draw(rw1.getBounds());
          
          if(endX1+(int)bCountX1>600||endX1+(int)bCountX1<0||startY1+(int)rw1.fireY()+(int)bCountY1>600||startY1+(int)rw1.fireY()+(int)bCountY1<0)
              {
              rw1.setFiring(false);
              rw1.changeBounds(-5, -5);
              }
      }
     
      if(rw2.firing())
      {
          bowCount2+=15;
          if (rw2.sideOfPlayer(p.getX()))
          {
              endX2 = startX2 +(int)rw2.fireX();
              bCountX2=rw2.firingX(bowCount2);
          } 
          else
          {
              endX2 = startX2 -(int)rw2.fireX();
              bCountX2=-(rw2.firingX(bowCount2));
          }
          
           bCountY2=(rw2.firingY(bowCount2));
          
          g2.drawLine(startX2+(int)bCountX2, startY2+(int)bCountY2, endX2+(int)bCountX2, startY2+(int)rw2.fireY()+(int)bCountY2);
          rw2.changeBounds(endX2+(int)bCountX2-1, startY2+(int)rw2.fireY()+(int)bCountY2-1);
          g2.draw(rw2.getBounds());
          
          if(endX2+(int)bCountX2>600||endX2+(int)bCountX2<0||endX2+(int)bCountX2>600||endX2+(int)bCountX2<0)
              {
              rw2.setFiring(false);
              rw2.changeBounds(-5, -5);
              }
          
      }
      }
      
      g2.setColor(Color.WHITE);
      Font font = new Font(Font.SANS_SERIF, Font.BOLD, 20);
      g2.setFont(font);
      g2.drawString("Level:"+levelDown+"    Monsters Killed:"+monstersKilled,250,20);
      g2.drawImage(life.getImage(), 0, 0, this);   

      
      //weapon x,y
      weaponX = w.getX();
      weaponY = w.getY();

      if(w.getUp()==true || w.getDown() ==true || w.getLeft()==true || w.getRight() ==true)
      {
    		  g2.drawImage(w.weapon(), weaponX, weaponY, this);
      }
      
   }
   
   public void actionPerformed(ActionEvent e)
   {
   if(inGame==true)
   {   

	   //obstacle intersect
	   for(int i=0; i<obstacleAmount; i++)
	      {
	         //if a player hits an obstacle
	      if(player.intersects(obstaclez.get(i).getBounds()))
	      {
	         if(harmCount==0)
	         {
	         p.stop();
	         life.kill();
	         
	         life.setImage();
	         harmCount++;
	         }
	         harmCount++;
	         if(harmCount>100)
	         {
	            harmCount=0;
	         }

	      }
	      //if you die the game ends
	      if(life.getHP()==0)
	      {
	         inGame=false;
	      }
	      }
	   
	      //if you die the game ends
	      if(life.getHP()==0)
	      {
	         inGame=false;
	      }

      
      /////DOOOOOR
      //door reset
      if(player.intersects(door.getBounds())&&killReach==monsterAmount)
      {
    	  p.setX(300);
    	  p.setY(500);
         levelDown++;
         //increases spawn amount based on your level
         if(levelcount==5)
         {
            spawnAmount+=5;
            levelcount=0;
         }
         
         monsters= new ArrayList<Monster>();
         
         for(int i=0; i<obstacleAmount; i++)
         {
            obstaclez.remove(0);
         }
         monsterAmount=rand.nextInt(5)+spawnAmount;
         obstacleAmount = rand.nextInt(3)+spawnAmount*2;
         //creates x amount of monsters
         for(int i=0; i<monsterAmount; i++)
         {
            //monsters
            
            creepers = new Monster();
            int s12, s13;
            do
            	{
            	 s12 = rand.nextInt(600);
                 s13 = rand.nextInt(600);
            	}
            while((s12>200&&s12<400)&&(s13>450));  
            creepers.setX(s12);
            creepers.setY(s13);
            monsters.add(creepers);
            
         }
         
         for(int i=0; i<obstacleAmount; i++)
         {
            obstacle = new Obstacles();
            //obstacle
            int z11, q11;
            do
            	{
            	 z11 = rand.nextInt(600);
                 q11 = rand.nextInt(600);
            	}
            while((z11>200&&z11<400)&&(q11>450));         
            obstacle.setX(z11);
            obstacle.setY(q11);
            obstaclez.add(obstacle);
         }

         door.setX(rand.nextInt(400)+100);
         door.setY(rand.nextInt(400)+100);
         levelcount++;
         killReach=0;

      }
      
      
    //Weapon damage
      w.changeBounds(w.getX(), w.getY());
      for(int i=0; i<monsters.size(); i++)
     {           
            if(monsters.get(i).getLife()==true)
         {
            monsters.get(i).changeBounds();
       if(w.getBounds().intersects(monsters.get(i).getBounds()))
       {
           monsters.get(i).kill();
           monstersKilled++;
           killReach++;
       }
         }
     }


	   
	 //bow damage
       for(int i=0; i<monsters.size(); i++)
      {           
             if(monsters.get(i).getLife()==true)
          {
             monsters.get(i).changeBounds();
        if(rw0.getBounds().intersects(monsters.get(i).getBounds()))
        {
            monsters.get(i).hurt();
            rw0.setFiring(false);
            rw0.changeBounds(-5, -5);
            if(monsters.get(i).alive==false)
            {
            	monstersKilled++;
            	killReach++;
            }
            	
        }
        else if(rw1.getBounds().intersects(monsters.get(i).getBounds()))
        {
            monsters.get(i).hurt();
            rw1.setFiring(false);
            rw1.changeBounds(-5, -5);
            if(monsters.get(i).alive==false)
            {
            	monstersKilled++;
            	killReach++;
            }
          }
        else if(rw2.getBounds().intersects(monsters.get(i).getBounds()))
        {
            monsters.get(i).hurt();
            rw2.setFiring(false);
            rw2.changeBounds(-5, -5);
            if(monsters.get(i).alive==false)
            {
            	monstersKilled++;
            	killReach++;
            }
        }
        
        
      }
      }

	   
	   
	   
	   
      //checks if you are injured
      p.changeBounds();
      player = p.getBounds();
      
      //as long as the monster is alive it goes through each one
      for(int i=0; i<monsters.size(); i++)
      {           
         if(monsters.get(i).getLife()==true)
      {
         monsters.get(i).changeBounds();
         //if a player touches a monster
      if(player.intersects(monsters.get(i).getBounds()))
      {
         if(harmCount==0)
         {
         p.stop();
         life.hurt();
         life.setImage();
         harmCount++;
         }
         harmCount++;
         if(harmCount>100)
         {
            harmCount=0;
         }
         
         
      }
     
      for(int yi=0; yi<obstacleAmount; yi++)
      {
         //if ia monster hits an obstacle
      if(monsters.get(i).getBounds().intersects(obstaclez.get(yi).getBounds()))
      {
         monsters.get(i).stop();
         monsters.get(i).createDirection();
         monsters.get(i).move();
        
      }
      }
      }
      }
      
     
      
      
      //determines creep's movement
      if(counterMONST>=50)
      {
         for(int i=0; i<monsters.size(); i++)
         {   
               monsters.get(i).createDirection();
               

         }
         counterMONST=0;
      }
      
      /////////PLAYER
      //corrects out of boundries
      if(playerX+p.getWidth() >615)
      {
         p.setX(615-p.getWidth());
         p.stop();
      }
      if(playerX <0)
      {
         p.setX(1);
         p.stop();
      }
      if(playerY+p.getHeight() >579)
      {
         p.setY(579-p.getHeight());
         p.stop();
      }
      if(playerY <0)
      {
         p.setY(1);
         p.stop();
      }
      
  
      
      
      //checks what image to load from player
      //down
      if(p.getUp()==false && p.getDown()==true && p.getLeft()==false && p.getRight()==false )
      {
         counter++;
         if(p.getFrame() >=3)
         {
            p.setFrame(0);
         }
         p.changeSprite();
         if (counter>15)
         {
         p.changeFrame();
         counter=0;
         }
      }
      
      //up
      else if(p.getUp()==true && p.getDown()==false && p.getLeft()==false && p.getRight()==false )
      {
         counter++;
         if(p.getFrame() >=6 || p.getFrame()<3)
         {
         p.setFrame(3);
         }
    
         p.changeSprite();
         if (counter>15)
         {
            p.changeFrame();
         counter=0;
         }
      }
      
      //left
      else  if(p.getUp()==false && p.getDown()==false && p.getLeft()==true && p.getRight()==false )
      {
         counter++;
         if(p.getFrame() >=9|| p.getFrame()<6)
         {
            p.setFrame(6);
         }
    
         p.changeSprite();
         if (counter>15)
         {
            p.changeFrame();
            counter=0;
         }
      }
      
      //right
      else if(p.getUp()==false &&p.getDown()==false && p.getLeft()==false && p.getRight()==true )
      {
         counter++;
         if(p.getFrame() >=12|| p.getFrame()<9)
         {
            p.setFrame(9);
         }
    
         p.changeSprite();
         if (counter>15)
         {
            p.changeFrame();
            counter=0;
         }
      }
      
      //up Right
      else if(p.getUp()==true && p.getDown()==false && p.getLeft()==false && p.getRight()==true )
      {
         counter++;
         if(p.getFrame() >=15|| p.getFrame()<12)
         {
            p.setFrame(12);
         }
    
         p.changeSprite();
         if (counter>15)
         {
            p.changeFrame();
            counter=0;
         }
      }
      
      //up left
      else if(p.getUp()==true && p.getDown()==false && p.getLeft()==true && p.getRight()==false )
      {  
         counter++;
         if(p.getFrame() >=18|| p.getFrame()<15)
         {
            p.setFrame(15);
         }
    
         p.changeSprite();
         if (counter>15)
         {
            p.changeFrame();
            counter=0;
            }
      }
      
      //down right
      else if(p.getUp()==false && p.getDown()==true && p.getLeft()==false && p.getRight()==true )
      {
         counter++;
         if(p.getFrame() >=21|| p.getFrame()<18)
         {
            p.setFrame(18);
         }
         p.changeSprite();
         if (counter>15)
         {
            p.changeFrame();
            counter=0;
         }
      }
      
      //down left
      else if(p.getUp()==false && p.getDown()==true && p.getLeft()==true && p.getRight()==false )
         { 
         counter++;
         if(p.getFrame() >=24|| p.getFrame()<21)
         {
         p.setFrame(21);
         }
    
         p.changeSprite();
         if (counter>15)
         {
            p.changeFrame();
            counter=0;
         }
      }
      //Keeps the image when nothing is pressed
      else
      {
         p.changeSprite();
      }
      //END PLAYER
      
      
      
      
      
      
      

      //Monster stuff, not sure where to put this
      //MONSTER
            //keeps it in the map
      for(int i=0; i<monsters.size(); i++)
      {
      if(monsters.get(i).getLife()==true)
      {
      if(monsters.get(i).getX()+monsters.get(i).getWidth() >599)
      {
         monsters.get(i).setX(599-monsters.get(i).getWidth());
         monsters.get(i).stop();
         monsters.get(i).createDirection();
      }
      if(monsters.get(i).getX() <0)
      {
         monsters.get(i).setX(1);
         monsters.get(i).stop();
         monsters.get(i).createDirection();
      }
      if(monsters.get(i).getY()+monsters.get(i).getHeight() >550)
      {
         monsters.get(i).setY(549-monsters.get(i).getHeight());
         monsters.get(i).stop();
         monsters.get(i).createDirection();
      }
      if(monsters.get(i).getY() <0)
      {
         monsters.get(i).setY(1);
         monsters.get(i).stop();
         monsters.get(i).createDirection();
      }
      }
      }
      for(int i=0; i<monsters.size(); i++)
      {
         if(monsters.get(i).getLife()==true)
         {
            //stop
            if(monsters.get(i).getDirection()==0 && monstcount<30)
            {
               monstcount=0;
               monsters.get(i).stop();
               
            }
            
            //up
            else if(monsters.get(i).getDirection()==1 && monstcount<30)
            {
               monsters.get(i).down();
               counter++;
               monstcount=0;
               
               if(monsters.get(i).getFrame() >=3)
               {
                  monsters.get(i).setFrame(0);
               }
               monsters.get(i).changeSprite();
               if(counter>15)
               {
               monsters.get(i).changeFrame();
               counter=0;
               }
            }
            //down
            else if(monsters.get(i).getDirection()==2&& monstcount<30)
            {
               monsters.get(i).up();
               counter++;
               monstcount=0;

               if(monsters.get(i).getFrame() >=6 || monsters.get(i).getFrame()<3)
               {
                  monsters.get(i).setFrame(3);
               }
               monsters.get(i).changeSprite();
               if(counter>15)
               {
               monsters.get(i).changeFrame();
               counter=0;
               }
            }
            //left
            else if(monsters.get(i).getDirection()==3&& monstcount<30)
            {
               monsters.get(i).left();
               counter++;
               monstcount=0;
               
               if(monsters.get(i).getFrame() >=9 || monsters.get(i).getFrame()<6)
               {
                  monsters.get(i).setFrame(6);
               }
               monsters.get(i).changeSprite();
               if(counter>15)
               {
               monsters.get(i).changeFrame();
               counter=0;
               }
            }
            //right
            else if(monsters.get(i).getDirection()==4&& monstcount<30)
            {
               monsters.get(i).right();
               counter++;
               monstcount=0;

               if(monsters.get(i).getFrame() >=12 || monsters.get(i).getFrame()<9)
               {
                  monsters.get(i).setFrame(9);
               }
               monsters.get(i).changeSprite();
               if(counter>15)
               {
               monsters.get(i).changeFrame();
               counter=0;
               }
            }
            
            

         }
         if(monstcount>30)
       	  monstcount=0;
      }


         /*
   */
    //weapon Image
      //up
        if(w.getUp()==true && w.getDown()==false && w.getLeft()==false && w.getRight()==false)
        {
            
               weapcount++;
                if(w.getFrame() >=3)
                {
                w.setFrame(0);
                }
           
                w.changeSprite();
                if (weapcount>10)
                {
                   w.changeFrame();
                   weaponframecount++;
                   weapcount=0;
                }  
            
                if(weaponframecount>2)
                {
                  w.setMoveRight(false);
                  w.setMoveLeft(false);
                  w.setMoveUp(false);
                  w.setMoveDown(false);
                  w.changeBounds(650,650);
                }
              
        }
        
        
        //right
        else if(w.getUp()==false &&w.getDown()==false && w.getLeft()==false && w.getRight()==true )
        {
            weapcount++;
            if(w.getFrame()<3|| w.getFrame()>=6)
            {
            w.setFrame(3);
            }
       
            w.changeSprite();
            if (weapcount>10)
            {
               w.changeFrame();
               weaponframecount++;
               weapcount=0;
            }  
        
          if(weaponframecount>2)
          {
              w.setMoveRight(false);
              w.setMoveLeft(false);
              w.setMoveUp(false);
              w.setMoveDown(false);
              w.changeBounds(650,650);
          }
              
        }
        
      //down
        else if(w.getUp()==false && w.getDown()==true && w.getLeft()==false && w.getRight()==false)
        {
            weapcount++;
            if(w.getFrame()<6|| w.getFrame()>=9)
            {
            w.setFrame(6);
            }
       
            w.changeSprite();
            if (weapcount>10)
            {
               w.changeFrame();
               weaponframecount++;
               weapcount=0;
            }  
        
            if(weaponframecount>2)
            {
              w.setMoveRight(false);
              w.setMoveLeft(false);
              w.setMoveUp(false);
              w.setMoveDown(false);
              w.changeBounds(650,650);
            }
        }
        
        //left
        else  if(w.getUp()==false && w.getDown()==false && w.getLeft()==true && w.getRight()==false)
        {
            weapcount++;
            if(w.getFrame() <9 || w.getFrame()>=12)
            {
            w.setFrame(9);
            }
       
            w.changeSprite();
            if (weapcount>10)
            {
               w.changeFrame();
               weaponframecount++;
               weapcount=0;
            }  
        
            if(weaponframecount>2)
            {
              w.setMoveRight(false);
              w.setMoveLeft(false);
              w.setMoveUp(false);
              w.setMoveDown(false);
              w.changeBounds(650,650);
            }
        }
        //makes sure if none of the statements above are true the factors are reset 
        else
             {
              w.setMoveRight(false);
              w.setMoveLeft(false);
              w.setMoveUp(false);
              w.setMoveDown(false);
              w.changeBounds(650,650);
             }


     
        


      playerX = p.getX();
      playerY= p.getY();
      counterMONST++;
      monstcount++;
      
      if(coolDown<100)
      coolDown+=5;
      
      for(int i=0; i<monsters.size(); i++)
      {
    	  
      monsters.get(i).move();
         
      }
      
      
      

//moves and paints
      p.move();
      repaint();
      
      
   }


   }

   public void keyPressed(KeyEvent e)
   {
      
      if(inGame==true)
      {
      //gets the key code from what is pressed, each key has its own code.
      int code = e.getKeyCode();
      //sees if any of these keys are pressed
      if(code==KeyEvent.VK_S)
      {
         p.setMoveDown(true);
         p.down();
         
      }
      
      if(code ==KeyEvent.VK_W)
      {
         p.setMoveUp(true);
         p.up();
      }
       
      if(code == KeyEvent.VK_A)
      {
         p.setMoveLeft(true);
         p.left();

      }
      
      if(code ==KeyEvent.VK_D)
      {
         p.setMoveRight(true);
         p.right();

      }
      
      if(p.getUp()==true && p.getDown()==false && p.getLeft()==false && p.getRight()==true )
      {
         p.upRight();

      }
      
      if(p.getUp()==true && p.getDown()==false && p.getLeft()==true && p.getRight()==false )
      {
         p.upLeft();

      }
      
      if(p.getUp()==false && p.getDown()==true&& p.getLeft()==false && p.getRight()==true )
      {
         p.downRight();
         
      }
      
      if(p.getUp()==false && p.getDown()==true && p.getLeft()==true && p.getRight()==false )
      {
         p.downLeft(); 
         
      }
      
      if(code==KeyEvent.VK_UP)
      { 
         w.setX(p.getX());
         w.setY(p.getY()-20);
         w.setMoveUp(true);
         weaponframecount=0;
      
      }
      
      if(code==KeyEvent.VK_RIGHT)
      {
         w.setX(p.getX()+p.getWidth()-12);
         w.setY(p.getY());
         w.setMoveRight(true);
         weaponframecount=0;
      
      }
      
      if(code==KeyEvent.VK_DOWN)
      {
         w.setX(p.getX());
         w.setY(p.getY()+p.getHeight());
         w.setMoveDown(true);
         weaponframecount=0;
       
      }
      
      if(code==KeyEvent.VK_LEFT)
      {
         w.setX(p.getX()-20);
         w.setY(p.getY());
         w.setMoveLeft(true);
         weaponframecount=0;
         
      }

      }
    
      
      }
   
   public void keyTyped(KeyEvent e )
   {
      
   }
   
   public void keyReleased(KeyEvent e)
   {
      if(inGame==true)
      {
      //gets the key code from what is pressed, each key has its own code.
      int code = e.getKeyCode();
      //sees if any of these keys are pressed

      if(code ==KeyEvent.VK_W)
      {
         p.setMoveUp(false);
         
         p.stopY();
         
         
      }
      
      if(code==KeyEvent.VK_S)
      {
         p.setMoveDown(false);
         
         p.stopY();
         
      }
      
      if(code == KeyEvent.VK_A)
      {
         p.setMoveLeft(false);
         
         p.stopX();
         
      }
      
      
      if(code ==KeyEvent.VK_D)
      {
         p.setMoveRight(false);
         
         p.stopX();
         
      }

      //weapon attack
      if(code==KeyEvent.VK_UP)
      {
         
      }
      
      if(code==KeyEvent.VK_RIGHT)
      {
   
      }
      
      if(code==KeyEvent.VK_DOWN)
      {

      }
      
      if(code==KeyEvent.VK_LEFT)
      {

      }
      

   }
   }
      
   public void mouseClicked(MouseEvent e) 
   {
	   if(inGame)
	   {
       if(coolDown==100)
       {
           if(arrowCount==1)
           {
               bowCount=0;
               rw0.setX(e.getX());
               rw0.setY(e.getY());
               rw0.fireEquation(p.getX(), p.getY());
               rw0.setFiring(true);
               startX=p.getX()+20;
               startY=p.getY()+20;
               coolDown=0;
           }
           if(arrowCount==2)
           {
               bowCount1=0;
               rw1.setX(e.getX());
               rw1.setY(e.getY());
               rw1.fireEquation(p.getX(), p.getY());
               rw1.setFiring(true);
               startX1=p.getX()+20;
               startY1=p.getY()+20;
               coolDown=0;
           }
           if(arrowCount==3)
           {
               bowCount2=0;
               rw2.setX(e.getX());
               rw2.setY(e.getY());
               rw2.fireEquation(p.getX(), p.getY());
               rw2.setFiring(true);
               startX2=p.getX()+20;
               startY2=p.getY()+20;
               coolDown=0;
           }
 
           arrowCount++;
           if (arrowCount >3)
               arrowCount=1;
       }
   }
       if(inGame==false)
       {
    	   if(e.getX()>200&&e.getX()<400&&e.getY()>100&&e.getY()<200)
    	   {
    		   inGame=true;
    		   life.setHP(3, 3);
    		   life.setImage();
    		   
    		   p.setX(300);
    	    	  p.setY(500);
    	         levelDown=0;
    	         spawnAmount=5;
    	         monsters= new ArrayList<Monster>();
    	         
    	         for(int i=0; i<obstacleAmount; i++)
    	         {
    	            obstaclez.remove(0);
    	         }
    	         monsterAmount=rand.nextInt(5)+spawnAmount;
    	         obstacleAmount = rand.nextInt(3)+spawnAmount*2;
    	         //creates x amount of monsters
    	         for(int i=0; i<monsterAmount; i++)
    	         {
    	            //monsters
    	            
    	            creepers = new Monster();
    	            int s12, s13;
    	            do
    	            	{
    	            	 s12 = rand.nextInt(600);
    	                 s13 = rand.nextInt(600);
    	            	}
    	            while((s12>200&&s12<400)&&(s13>450));  
    	            creepers.setX(s12);
    	            creepers.setY(s13);
    	            monsters.add(creepers);
    	            
    	         }
    	         
    	         for(int i=0; i<obstacleAmount; i++)
    	         {
    	            obstacle = new Obstacles();
    	            //obstacle
    	            int z11, q11;
    	            do
    	            	{
    	            	 z11 = rand.nextInt(600);
    	                 q11 = rand.nextInt(600);
    	            	}
    	            while((z11>200&&z11<400)&&(q11>450));         
    	            obstacle.setX(z11);
    	            obstacle.setY(q11);
    	            obstaclez.add(obstacle);
    	         }

    	         door.setX(rand.nextInt(400)+100);
    	         door.setY(rand.nextInt(400)+100);
    	         levelcount=0;
    	         
    	         p.setMoveDown(false);
    	         p.setMoveLeft(false);
    	         p.setMoveRight(false);
    	         p.setMoveUp(false);
    	         monstersKilled=0;
    	         killReach=0;
    	   }
       }
    	   
	   
   }




      @Override
      public void mouseEntered(MouseEvent e) {
      	// TODO Auto-generated method stub
      	
      }




      @Override
      public void mouseExited(MouseEvent e) {
      	// TODO Auto-generated method stub
      	
      }




      @Override
      public void mousePressed(MouseEvent e) {
      	// TODO Auto-generated method stub
      	
      }




      @Override
      public void mouseReleased(MouseEvent e) 
      {
      	
      }
   }

   

